import Vue from "vue";

//配置路由相关信息
import VueRouter from "vue-router";

import store from "../store/index";

import Login from "../views/main/Login";
import Register from "../views/main/Register";
import RegCompany from "../views/main/register/Company"
import RegUniversity from "../views/main/register/School"

// 页面刷新时，重新赋值token
if (sessionStorage.getItem("token")) {
  store.commit("set_token", sessionStorage.getItem("token"));
}

//1.通过Vue.use(插件),安装插件
Vue.use(VueRouter);

//2.创建路由对象
const routes = [
  {
    path: "",
    redirect: "/main",
  },
  {
    path:'/setting',
    component:()=>import("../views/Setting/index"),
    children:[
      {
        path:'',
        redirect:'myArchives'
      },
      {
        path:'myArchives',
        component: ()=>import("../views/Setting/archives")
      },
      {
        path: 'setting',
        component: ()=>import("../views/Setting/setting")
      },
      {
        path:'myDynamic',
        component: ()=>import("../views/Setting/myDynamic")
      },
      {
        path:'myConcern',
        component: ()=>import("../views/Setting/myConcern")
      },
      {
        path:'myCollection',
        component: ()=>import("../views/Setting/myCollection")
      }
    ]
  },
  {
    path:'/admin',
    component:()=>import("../views/Admin/index"),
    children: [
      {
        path:'',
        redirect: 'university'
      },
      {
        path:'university',
        component: ()=>import("../views/Admin/University/index"),
        children: [
          {
            path: '',
            redirect: 'stuList',
          },

          {
            path:'stuList',
            component: ()=>import("../views/Admin/University/stuList")
          },
          {
            path:'addStu',
            component: ()=>import("../views/Admin/University/addStu")
          }
        ]
      },
      {
        path:'company',
        component: ()=>import("../views/Admin/Company/index"),
        children: [
          {
            path: '',
            redirect: 'video',
          },
          {
            path:'video',
            component:()=>import("../views/Admin/Company/video")
          },
          {
            path:'practice',
            component:()=>import("../views/Admin/Company/practice")
          },
          {
            path:'uploadVideo',
            component:()=>import("../views/Admin/Company/uploadVideo")
          }
        ]
      }
    ]
  },
  {
    path:'/home',
    component:()=>import("../views/Home/index"),
    children: [
      {
        path:'',
        redirect: 'dynamic'
      },
      {
        path:'recommend',
        component: ()=>import("../views/Home/Recommend/index")
      },
      {
        path:'Player',
        component:()=>import("../views/Player/index")
      },
      {
        path:'platforms',
        component: ()=>import("../views/Home/PlatForms/index")
      },
      {
        path:'companyVideo',
        component: ()=>import("../views/Home/Video/company")
      },
      {
        path:'recruitment',
        component: ()=>import("../views/Home/Recruitment/index")
      },
      {
        path:'dynamic',
        component: ()=>import("../views/Home/Dynamic/index"),
        meta: {
          title: '动态'
        }
      },
      {
        path:'companyPractice',
        component: ()=>import("../views/Home/Practice/company"),
        meta:{
          title: '练习'
        }

      }
    ]
  },
  {
    path:'/other',
    component:()=>import("../components/content/UserArchives"),
    children: [
      {
        path: "",
        redirect: 'archives'
      },
      {
        path:'archives',
        component: ()=>import("../views/Setting/archives")
      },
      {
        path:'dynamic',
        component: ()=>import("../views/Setting/myDynamic")
      }
    ]
  },
  {
    path: "/main",
    component: () => import("../views/main/Main"),
    children: [
      {
        path: "",
        redirect: "login",
      },
      {
        path: "login",
        name: "adminLogin",
        component: Login,
        meta: {
          title: "登录",
        },
      },
      {
        path: "register",
        component: Register,
        children: [
          {
            path: "",
            redirect: "company",
          },
          {
            path: "company",
            component: RegCompany,
            meta: {
              title: "企业注册",
            },
          },
          {
            path: "university",
            component: RegUniversity,
            meta: {
              title: "学校注册",
            },
          },
        ],
      },
    ],
  },

];

const router = new VueRouter({
  //配置路由和组件之间的关系
  routes,
  mode: "history",
  linkActiveClass: "active",
});

// 前置守卫(guard)
router.beforeEach((to, from, next) => {
  if (to.matched.some((r) => r.meta.requireAuth)) {
    //这里的requireAuth为路由中定义的 meta:{requireAuth:true}，意思为：该路由添加该字段，表示进入该路由需要登陆的
    if (store.getters.token) {
      next();
      document.title = to.meta.title;
      let matched=to.matched
      console.log(to.matched);
      let list=[];
      matched.forEach(element => {
        list.push({"title":element.meta.title,"path": element.path})
      });
      store.dispatch("user/setBreads",list)
    } else {
      next({
        path: "/login",
        query: { redirect: to.fullPath },
      });
    }
  } else {
    next();
  }
});



//后置钩子(hook)
router.afterEach((route) => {
  console.log(2);
});

//解决两次访问相同路由报错
const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
  return originalPush.call(this, location).catch((err) => err);
};

// 3.将router对象传到vue实例
export default router;

export function resetRouter() {
  const newRouter = createRouter();
  router.matcher = newRouter.matcher; // reset router
}
